package controllers;

import java.util.*;

import models.*;

import com.avaje.ebean.*;

import play.libs.Yaml;

import play.mvc.*;

import static play.data.Form.*;

import views.html.*;

public class Application extends Controller {
	
	
	//log in
    public static Result login() {
    	
    	session().clear();
    	
    	@SuppressWarnings("unchecked")
		Map<String,List<Object>> all = (Map<String,List<Object>>)Yaml.load("test-data2.yml");
    	
    	if(Classifica.all().isEmpty())
     		Ebean.save(all.get("classifiche"));
    	if(Tipo.all().isEmpty())
    		Ebean.save(all.get("tipi"));
    	if(Categoria.all().isEmpty())
    		Ebean.save(all.get("categorie"));
    	if(User.all().isEmpty())
    		Ebean.save(all.get("users"));
    	
        return ok(login.render(form(User.class)));
    }
    
    //log out
    public static Result logout() {
        session().clear();
        return redirect(
            routes.Application.login()
        );
    }
	
	
	//menu principale
    
    
    @Security.Authenticated(Secured.class)
	public static Result home() {
    	return ok(home.render());
    }
    
    
    //mostra tabelle
    @Security.Authenticated(Secured.class)
    public static Result index() {
        return ok(index.render("index"));
    }
   
}
